package com.hicorp.segment.controller;

import com.hicorp.segment.service.BaseInterface;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/basic-request/{tableName}")
public class BasicController {
    private final BaseInterface baseInterface;

    public BasicController(BaseInterface baseInterface) {
        this.baseInterface = baseInterface;
    }

    /**
     * 单表查询所有
     *
     * @param tableName 表名
     * @return 统一返还类
     */
    @GetMapping
    public Object selectAll(@PathVariable String tableName) {
        return baseInterface.queryAll(tableName);
    }

    /**
     * 组合查询
     * @param tableName 表名
     * @param record 传入的查询条件
     * @return 查询的结果, 使用统一返还类封装的.
     */
    @PostMapping("/search")
    public Object searchRecord(@PathVariable String tableName, @RequestBody(required = false) Object record) {
        return baseInterface.searchRecord(tableName, record);
    }
    /**
     * 单表新增单条数据
     *
     * @param tableName 表名
     * @param record    数据
     * @return 统一返还类
     */
    @PostMapping
    public Object createRecord(@PathVariable String tableName, @RequestBody Object record) {
        return baseInterface.insert(tableName, record);
    }

    @PostMapping("/batch")
    public Object batchInsert(@PathVariable String tableName, @RequestBody List<Object> recordList) {
        return baseInterface.insertList(tableName, recordList);
    }

    // 基础表修改
    @PutMapping("/{id}")
    public Object updateRecord(@PathVariable String tableName, @PathVariable Long id, @RequestBody Object record) {
        return baseInterface.update(tableName, id, record);
    }

    // 基础表批量删除
    @PostMapping("/batch-delete")
    public Object batchDelete(@PathVariable String tableName, @RequestBody List<Long> ids) {
        return baseInterface.batchDeleteRecord(tableName, ids);
    }

    // 基础表搜索
    @GetMapping("/{field}/{content}")
    public Object selectRecord(@PathVariable String tableName, @PathVariable String field, @PathVariable String content) {
        return baseInterface.selectRecords(tableName, field, content);
    }
}
